/**
 *
 * @param {*} title1 title1数组
 * @param {*} title2 title2数组
 * @param {*} offSetIndex 去除前几项数据，需要+1
 * @param {*} defaultFontSize 默认一个文字占位大小，默认为10
 * @param {*} widthOffset 计算宽度偏移量 默认10
 * @param {*} defaultHeight 默认高度值 25
 * @param {*} defaultWidth  默认宽度值 100
 * @param {*} minWidth  最小宽度值 60
 * @returns
 */
export function dealTitleWidthAndHeight(
  title1,
  title2,
  offSetIndex,
  defaultFontSize = 10,
  widthOffset = 10,
  defaultHeight = 25,
  defaultWidth = 100,
  minWidth = 60
) {
  let tempTitle1 = title1.splice(offSetIndex);
  let tempTitle2 = title2.splice(offSetIndex);
  let screenWidth = document.body.clientWidth; //获取屏幕宽度

  console.log(tempTitle1);
  console.log(tempTitle2);
  //2.计算title2的单元格宽高
  let title1Data = [],
    title2Data = [];
  // 判断title2的长度，如果小于4，就平分屏幕
  let isAverageWidth = tempTitle2.length < 4;
  let averageWidth = (screenWidth - 100) / tempTitle2.length;

  //表格最大宽度,默认单元格高度
  let maxWidth = isAverageWidth ? averageWidth : defaultWidth,
    title2Height = defaultHeight;

  //2.1 循环计算title2的单元格的宽高
  for (let index = 0; index < tempTitle2.length; index++) {
    let title2Dict = {};
    const element = tempTitle2[index];
    title2Dict.name = element;
    var width = getStrLength(element) * defaultFontSize;
    //对于宽度太小的，给个固定的宽度
    width = width < minWidth ? minWidth : width;
    var height = defaultHeight;
    //如果计算的宽度大于单元格最大宽度，换行处理
    if (width > maxWidth) {
      height = defaultHeight * Math.ceil(width / maxWidth);
      //保证title2单元格的高度保持一致
      title2Height = title2Height >= height ? title2Height : height;
      width = maxWidth;
    }
    title2Dict.width = width + widthOffset;
    title2Dict.height = title2Height;
    title2Data.push(title2Dict);
  }
  console.log("title1");

  console.log(title2Data);
  //3. 循环计算title1的单元格的宽高
  //3.1 如果title1为空的话
  if (title1.length == 1) {
    let title1Dict = { width: 0, height: 0, name: null };
    title1Data.push(title1Dict);
  } else {
    //3.1 如果title1不为空的话,需要找到数组数据重复数据的起始位置和长度
    let sameData = findArraySameDataWithIndex(tempTitle1);
    console.log(sameData);
    //3.2 循环取出title2中的重复数据的起始位置和长度
    for (let index1 = 0; index1 < sameData.length; index1++) {
      const sameItem = sameData[index1];
      //3.3 根据title1的宽度来计算title1的宽度
      let title1Dict = {},
        width = 0;

      //3.4 如果title1的长度为1，需要单独计算下title1的宽度
      let onlyOneTitle1Width = 0;
      if (sameItem.length == 1) {
        onlyOneTitle1Width = getStrLength(sameItem.name) * 10;
      }

      //3.5 根据数据的起始位置和长度来取出title2中对应的数据
      let index2Length = sameItem.startIndex + sameItem.length;
      for (let index2 = sameItem.startIndex; index2 < index2Length; index2++) {
        console.log("index2=" + index2 + "总长度" + index2Length);
        const title2Item = title2Data[index2];
        title2Item.width =
          title2Item.width > onlyOneTitle1Width
            ? title2Item.width
            : onlyOneTitle1Width;
        width += title2Item.width;
      }

      title1Dict.width = width;
      title1Dict.height = defaultHeight + 10;
      title1Dict.name = sameItem.name;
      title1Data.push(title1Dict);
    }
  }
  console.log(title2Data);
  console.log("title1Data");

  console.log(title1Data);
  //4. 计算区域(总表头)高度
  let title1Item = title1Data[0];
  let areaHeight = title1Item.height + title2Height;
  return {
    title1Data: title1Data,
    title2Data: title2Data,
    areaHeight: areaHeight,
  };
}

/**
 *
 * @param {*} list 需要处理的列表数据
 * @param {*} title2Data title2处理后的数据
 * @param {*} offSetIndex 去除前几项数据，需要+1
 * @param {*} defaultFontSize 默认一个文字占位大小，默认为10
 * @param {*} defaultAreaWidth 左侧区域默认宽度100, 高度28
 * @param {*} defaultAreaHeight  左侧区域默认宽度100, 高度28
 * @param {*} areaIdIndex 地市id在数组的角标
 * @param {*} areaNameIndex 地市name在数组的角标
 * @param {*} areaLevelIndex 地市层级level在数组的角标,如果areaLevelIndex = -1，说明接口没有返回areaLevel,需要自己传下面两个参数
 * @param {*} level 当前账号level层级 默认为0
 * @param {*} levelsLength 当前下钻数组保存的level数组长度 默认为0
 */
export function dealAreasWidthAndHeight(
  list,
  title2Data,
  offSetIndex,
  defaultFontSize = 10,
  defaultAreaWidth = 100,
  defaultAreaHeight = 30,
  areaIdIndex,
  areaNameIndex,
  areaLevelIndex,
  level = 0,
  levelsLength = 0
) {
  let leftData = [],
    rightData = [];

  for (let index = 0; index < list.length; index++) {
    const element = list[index];
    let areaItem = {};
    let width = getStrLength(element[areaNameIndex].value) * defaultFontSize;
    let height = defaultAreaHeight;
    //1. 计算leftData
    //1.1 对于宽度超过的默认宽度的，换行处理，不足的按照默认宽度处理
    if (width > defaultAreaWidth) {
      height = Math.ceil(width / defaultAreaWidth) * defaultAreaHeight;
    }
    width = defaultAreaWidth;

    areaItem.areaId = element[areaIdIndex].value;
    areaItem.areaName = element[areaNameIndex].value;
    if (areaLevelIndex == -1) {
      areaItem.areaLevel = level + levelsLength;
    } else {
      areaItem.areaLevel = element[areaLevelIndex].value;
    }

    areaItem.width = width;
    areaItem.height = height;

    leftData.push(areaItem);

    //2.计算rightData
    let rightSmallData = [];
    for (let index2 = offSetIndex; index2 < element.length; index2++) {
      const valueItem = element[index2];
      //2.1 获取title2的宽度
      const title2Item = title2Data[index2 - offSetIndex];
      //value宽度=表头对应的宽度
      valueItem.width = title2Item.width;
      //value高度=地区的高度
      valueItem.height = areaItem.height;

      rightSmallData.push(valueItem);
    }
    rightData.push(rightSmallData);
  }
  console.log(leftData);
  // console.log(rightData)
  return {
    leftData: leftData,
    rightData: rightData,
  };
}

/**
 * ------------------------------------------内部使用方法
 */

/**
 * 计算数组相同元素的起始位置和相同元素的长度
 * @param {*} array
 */
function findArraySameDataWithIndex(array) {
  let length = 1,
    startIndex = 0,
    sameData = [];
  for (let index = 0; index < array.length; index++) {
    const element = array[index];
    const afElement = index == array.length - 1 ? null : array[index + 1];
    if (element == afElement) {
      length = length + 1;
      if (index == array.length - 1) {
        let sameDict = {};
        sameDict.startIndex = startIndex;
        sameDict.length = length;
        sameDict.name = element;
        sameData.push(sameDict);
      }
    } else {
      let sameDict = {};
      sameDict.startIndex = startIndex;
      sameDict.length = length;
      sameDict.name = element;
      sameData.push(sameDict);

      //重置角标和长度
      startIndex = index + 1;
      length = 1;
    }
  }
  return sameData;
}

function getStrLength(str) {
  var len = 0;
  for (var i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94) {
      len += 2;
    } else {
      len++;
    }
  }
  if (len < 11) {
    return len * 1.1;
  } else if (len < 15) {
    return len / 1.1;
  } else {
    return len / 1.3;
  }
}
